<template>
  <div class="exhibition-intro">
    <div
      class="page-header image"
      :style="bannerImage ? { backgroundImage: `url(${bannerImage})` } : {}"
    >
      <div class="header-overlay">
        <div class="container">
          <h1 class="page-title">{{ pageData.title }}</h1>
          <p class="page-subtitle">
            {{
              pageData.subtitle ||
              "了解FAIR plus机器人全产业链接会的背景和发展历程"
            }}
          </p>
        </div>
      </div>
    </div>
    <!-- 导航菜单 -->
    <div class="nav-menu">
      <div class="container">
        <nav class="about-nav">
          <a
            @click="setActiveTab('exhibition-intro')"
            :class="['nav-item', { active: activeTab === 'exhibition-intro' }]"
          >
            展会介绍
          </a>
          <a
            @click="setActiveTab('product-scope')"
            :class="['nav-item', { active: activeTab === 'product-scope' }]"
          >
            展品范围
          </a>
          <a
            @click="setActiveTab('exhibition-highlights')"
            :class="[
              'nav-item',
              { active: activeTab === 'exhibition-highlights' },
            ]"
          >
            展会集锦
          </a>
          <a
            @click="setActiveTab('venue-intro')"
            :class="['nav-item', { active: activeTab === 'venue-intro' }]"
          >
            展馆介绍
          </a>
          <a
            @click="setActiveTab('organizer-intro')"
            :class="['nav-item', { active: activeTab === 'organizer-intro' }]"
          >
            主办方介绍
          </a>
          <a
            @click="setActiveTab('organization-structure')"
            :class="[
              'nav-item',
              { active: activeTab === 'organization-structure' },
            ]"
          >
            组织架构
          </a>
          <a
            @click="setActiveTab('past-exhibitions')"
            :class="['nav-item', { active: activeTab === 'past-exhibitions' }]"
          >
            往届展会
          </a>
          <a
            @click="setActiveTab('downloads')"
            :class="['nav-item', { active: activeTab === 'downloads' }]"
          >
            下载中心
          </a>
        </nav>
      </div>
    </div>

    <div class="container">
      <section class="intro-section">
        <div class="intro-content">
          <div class="intro-text">
            <div class="img-cover" v-if="pageData.imageUrl">
              <div class="intro-image">
                <img
                  :src="pageData.imageUrl"
                  :alt="pageData.title || '展会介绍'"
                />
              </div>
            </div>
            <div class="intro-description" v-html="pageData.content"></div>
          </div>
        </div>
      </section>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from "vue";
import { subpageApi } from "@/api/user";

export default {
  name: "ExhibitionIntro",
  setup() {
    const activeTab = ref("exhibition-intro");
    const pageData = ref({});
    const bannerImage = ref("");

    const parseContent = (content) => {
      if (!content) return {};
      try {
        if (typeof content === "string" && content.trim().startsWith("{")) {
          return JSON.parse(content);
        }
        return { content: content };
      } catch (e) {
        console.log("解析content字段失败:", e);
        return { content: content };
      }
    };

    const loadPageData = async () => {
      try {
        const response = await subpageApi.getSubpageByKey(
          "about",
          "exhibition-intro"
        );
        console.log(response, "saasad");

        if (response && response.data) {
          const parsed = parseContent(response.data.content);
          pageData.value = {
            title: response.data.title || "展会介绍",
            subtitle:
              response.data.subtitle ||
              "了解FAIR plus机器人全产业链接会的背景和发展历程",
            content: parsed.content || response.data.content || "暂无内容",
            imageUrl: parsed.imageUrl || response.data.imageUrl || "",
          };
        }
      } catch (error) {
        console.error("加载展会介绍数据失败:", error);
        pageData.value = {
          title: "展会介绍",
          subtitle: "了解FAIR plus机器人全产业链接会的背景和发展历程",
          content:
            "<p>FAIR plus机器人全产业链接会始创于2010年，是华南地区规模较大的机器人产业展览活动之一。</p>",
        };
      }
    };

    const loadBanner = async () => {
      try {
        const resp = await subpageApi.getContentByType("carousel_about");
        if (
          resp &&
          resp.code === "200" &&
          Array.isArray(resp.data) &&
          resp.data.length
        ) {
          const first =
            resp.data.find((it) => it.status === "active") || resp.data[0];
          const content =
            typeof first.content === "string"
              ? JSON.parse(first.content || "{}")
              : first.content || {};
          bannerImage.value = first.imageUrl || content.image || "";
        }
      } catch (e) {
        bannerImage.value = "";
      }
    };

    const setActiveTab = (tabKey) => {
      if (!tabKey) return;
      activeTab.value = tabKey;
      // 跳转到对应的子页面
      if (tabKey !== "exhibition-intro") {
        window.location.href = `/${tabKey}`;
      }
    };

    onMounted(() => {
      loadPageData();
      loadBanner();
    });

    return {
      activeTab,
      pageData,
      bannerImage,
      setActiveTab,
    };
  },
};
</script>

<style scoped>
.exhibition-intro {
  max-width: 100%;
  overflow: hidden;
  min-height: 100vh;
  background: #f8f9fa; /* 与关于我们页面一致的浅灰背景 */
  /* padding-top: 70px; 与其他页面顶部间距一致，避免被导航遮挡 */
}
:deep(.resizable-image) {
  max-width: 100% !important;
  height: auto !important;
}
.page-header {
  background: linear-gradient(135deg, #320063 0%, #4a0a8a 100%);
  color: white;
  padding: 80px 0;
  text-align: center;
}

.page-header.image {
  position: relative;
  background: none;
  padding: 0;
  height: 400px;
  background-size: cover;
  background-position: center;
}
.header-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  display: flex;
  align-items: center;
}

.page-title {
  font-size: 48px; /* 与其他页面一致 */
  margin-bottom: 20px;
}

.page-subtitle {
  font-size: 18px; /* 与其他页面一致 */
  opacity: 0.9;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.intro-section {
  padding: 60px 0;
}

.intro-content {
  background: white;
  border-radius: 16px;
  padding: 40px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid #e8eaed;
}

.intro-text {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 30px;
}

.img-cover {
  width: 100%;
  max-width: 800px;
  overflow: hidden;
}

.intro-image {
  width: 100%;
  padding-bottom: 56.25%; /* 16:9 比例 */
  position: relative;
}

.intro-image img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  /* max-width: 800px;
  height: auto; */
  display: block;
  border-radius: 12px;
}

.intro-description {
  width: 100%;
  max-width: 800px;
  text-align: left;
  line-height: 1.8;
  color: #333;
  font-size: 1.05rem;
}

.intro-description :deep(p) {
  margin-bottom: 16px;
}

/* 富文本内图片限制 */
.intro-description :deep(img) {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 10px 0;
  border-radius: 8px;
}

@media (max-width: 768px) {
  .page-title {
    font-size: 2rem;
  }
  .page-subtitle {
    font-size: 1rem;
  }
  .intro-content {
    padding: 24px;
  }
}

/* 导航菜单样式 - 与关于我们页面保持一致 */
.nav-menu {
  background: #f8f9fa;
  padding: 20px 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.about-nav {
  display: flex;
  justify-content: center;
  gap: 30px;
  flex-wrap: wrap;
}

.nav-item {
  text-decoration: none;
  color: #333;
  font-size: 1.1rem;
  font-weight: 500;
  padding: 10px 20px;
  border-radius: 8px;
  transition: background-color 0.3s ease, color 0.3s ease;
  border: 1px solid #eee;
  cursor: pointer;
}

.nav-item:hover {
  background-color: var(--e-global-color-selected);
  color: white;
  border-color: var(--e-global-color-selected);
}

.nav-item.active {
  background-color: var(--e-global-color-selected);
  color: white;
  border-color: var(--e-global-color-selected);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .about-nav {
    gap: 15px;
  }

  .nav-item {
    font-size: 1rem;
    padding: 8px 16px;
  }
}
</style>
